#region License
//
// Author: Ivan Porto Carrero <ivan@flanders.co.nz>
// Copyright (c) 2008, Flanders International Marketing Ltd.
// Copyright (c) 2007-2008, Enkari, Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#endregion
#region Usings
using System;
using Ninject.Core.Logging;
using NLog;
#endregion

namespace Ninject.Integration.NLog.Infrastructure
{
	/// <summary>
	/// A logger that integrates with nlog, passing all messages to a <see cref="Logger"/>.
	/// </summary>
	public class NLogLogger : LoggerBase
	{
		private readonly Logger _nlogLogger;

		/// <summary>
		/// Gets a value indicating whether messages with Debug severity should be logged.
		/// </summary>
		public override bool IsDebugEnabled
		{
			get { return _nlogLogger.IsDebugEnabled; }
		}

		/// <summary>
		/// Gets a value indicating whether messages with Info severity should be logged.
		/// </summary>
		public override bool IsInfoEnabled
		{
			get { return _nlogLogger.IsInfoEnabled; }
		}

		/// <summary>
		/// Gets a value indicating whether messages with Warn severity should be logged.
		/// </summary>
		public override bool IsWarnEnabled
		{
			get { return _nlogLogger.IsWarnEnabled; }
		}

		/// <summary>
		/// Gets a value indicating whether messages with Error severity should be logged.
		/// </summary>
		public override bool IsErrorEnabled
		{
			get { return _nlogLogger.IsErrorEnabled; }
		}

		/// <summary>
		/// Gets a value indicating whether messages with Fatal severity should be logged.
		/// </summary>
		public override bool IsFatalEnabled
		{
			get { return _nlogLogger.IsFatalEnabled; }
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="NLogLogger"/> class.
		/// </summary>
		/// <param name="type">The type to associate with the logger.</param>
		public NLogLogger(Type type)
			: base(type)
		{
		    _nlogLogger = LogManager.GetLogger(type.FullName);
		}

		/// <summary>
		/// Logs the specified exception with Debug severity.
		/// </summary>
		/// <param name="exception">The exception to log.</param>
		/// <param name="format">The message or format template.</param>
		/// <param name="args">Any arguments required for the format template.</param>
		public override void Debug(Exception exception, string format, object[] args)
		{
			_nlogLogger.DebugException(string.Format(format, args), exception);
		}

		/// <summary>
		/// Logs the specified message with Debug severity.
		/// </summary>
		/// <param name="format">The message or format template.</param>
		/// <param name="args">Any arguments required for the format template.</param>
		public override void Debug(string format, object[] args)
		{
			_nlogLogger.Debug(format, args);
		}

		/// <summary>
		/// Logs the specified exception with Error severity.
		/// </summary>
		/// <param name="exception">The exception to log.</param>
		/// <param name="format">The message or format template.</param>
		/// <param name="args">Any arguments required for the format template.</param>
		public override void Error(Exception exception, string format, object[] args)
		{
			_nlogLogger.ErrorException(string.Format(format, args), exception);
		}

		/// <summary>
		/// Logs the specified message with Error severity.
		/// </summary>
		/// <param name="format">The message or format template.</param>
		/// <param name="args">Any arguments required for the format template.</param>
		public override void Error(string format, object[] args)
		{
			_nlogLogger.Error(format, args);
		}

		/// <summary>
		/// Logs the specified message with Fatal severity.
		/// </summary>
		/// <param name="format">The message or format template.</param>
		/// <param name="args">Any arguments required for the format template.</param>
		public override void Fatal(string format, object[] args)
		{
			_nlogLogger.Fatal(format, args);
		}

		/// <summary>
		/// Logs the specified exception with Fatal severity.
		/// </summary>
		/// <param name="exception">The exception to log.</param>
		/// <param name="format">The message or format template.</param>
		/// <param name="args">Any arguments required for the format template.</param>
		public override void Fatal(Exception exception, string format, object[] args)
		{
			_nlogLogger.FatalException(string.Format(format, args), exception);
		}

		/// <summary>
		/// Logs the specified message with Info severity.
		/// </summary>
		/// <param name="format">The message or format template.</param>
		/// <param name="args">Any arguments required for the format template.</param>
		public override void Info(string format, object[] args)
		{
			_nlogLogger.Info(format, args);
		}

		/// <summary>
		/// Logs the specified exception with Info severity.
		/// </summary>
		/// <param name="exception">The exception to log.</param>
		/// <param name="format">The message or format template.</param>
		/// <param name="args">Any arguments required for the format template.</param>
		public override void Info(Exception exception, string format, object[] args)
		{
			_nlogLogger.InfoException(string.Format(format, args), exception);
		}

		/// <summary>
		/// Logs the specified message with Warn severity.
		/// </summary>
		/// <param name="format">The message or format template.</param>
		/// <param name="args">Any arguments required for the format template.</param>
		public override void Warn(string format, object[] args)
		{
			_nlogLogger.Warn(format, args);
		}

		/// <summary>
		/// Logs the specified exception with Warn severity.
		/// </summary>
		/// <param name="exception">The exception to log.</param>
		/// <param name="format">The message or format template.</param>
		/// <param name="args">Any arguments required for the format template.</param>
		public override void Warn(Exception exception, string format, object[] args)
		{
			_nlogLogger.WarnException(string.Format(format, args), exception);
		}
	}
}
